package com.hllxd.hism.star.basic.service.util;

import com.google.common.base.Strings;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author wuguangxi
 * @description
 * @date 2023/11/14 17:10
 **/
public class DateUtil {

    public static String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static String DATE_FORMAT = "yyyy-MM-dd";

    public DateUtil() {
    }

    public static long previous(int days) {
        return System.currentTimeMillis() - (long)days * 3600000L * 24L;
    }

    public static String formatDateTime(long d) {
        return (new SimpleDateFormat(DATETIME_FORMAT)).format(d);
    }

    public static String formatDate(long d) {
        return (new SimpleDateFormat(DATE_FORMAT)).format(d);
    }

    public static long parseDate(String d) {
        try {
            return (new SimpleDateFormat(DATE_FORMAT)).parse(d).getTime();
        } catch (Exception var2) {
            return 0L;
        }
    }

    public static Date getDate( String dateStr, String format ){

        SimpleDateFormat sdf = new SimpleDateFormat( format );
        try{
            return sdf.parse( dateStr );
        }
        catch( ParseException e ){
            return null;
        }
    }

    public static String getDateStr( Date date, String format ){

        if( date == null || org.apache.logging.log4j.util.Strings.isEmpty( format ) ){
            return "";
        }
        try{
            SimpleDateFormat sdf = new SimpleDateFormat( format );
            return sdf.format( date );
        }
        catch( Exception e ){
            return "";
        }
    }

    public static long parseDateTime(String dt) {
        try {
            return (new SimpleDateFormat(DATETIME_FORMAT)).parse(dt).getTime();
        } catch (Exception var2) {
            return 0L;
        }
    }

    public static Date getMinuteTime(Date date) {
        long seconds = date.getTime();
        seconds /= 1000L;
        long residue = seconds % 60L;
        seconds = (seconds - residue) * 1000L;
        date.setTime(seconds);
        return date;
    }

    public static int getDayBetweenDateTime(String date1, String date2){
        if(Strings.isNullOrEmpty(date1) || Strings.isNullOrEmpty(date2)){
            return 0;
        }
        long d1 = parseDateTime(date1);
        long d2 = parseDateTime(date2);
        return (int) ((d2 - d1) / (24 * 60 * 60 * 1000));
    }
}
